package com.luckcoder.relax.games.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luckcoder.relax.games.domain.RelaxGameConfig;
import com.luckcoder.relax.games.domain.bo.RelaxGameConfigBo;
import com.luckcoder.relax.games.domain.vo.RelaxGameConfigVo;
import com.luckcoder.relax.games.mapper.RelaxGameConfigMapper;
import com.luckcoder.relax.games.service.IRelaxGameConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 小游戏配置Service业务层处理
 *
 * @author zhanghao
 * @date 2025-09-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RelaxGameConfigServiceImpl implements IRelaxGameConfigService {
    
    private final RelaxGameConfigMapper baseMapper;
    
    /**
     * 查询小游戏配置
     *
     * @param gameId 主键
     * @return 小游戏配置
     */
    @Override
    public RelaxGameConfigVo queryById(Long gameId) {
        return baseMapper.selectVoById(gameId);
    }
    
    /**
     * 分页查询小游戏配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 小游戏配置分页列表
     */
    @Override
    public TableDataInfo<RelaxGameConfigVo> queryPageList(RelaxGameConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RelaxGameConfig> lqw = buildQueryWrapper(bo);
        Page<RelaxGameConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    
    /**
     * 查询符合条件的小游戏配置列表
     *
     * @param bo 查询条件
     * @return 小游戏配置列表
     */
    @Override
    public List<RelaxGameConfigVo> queryList(RelaxGameConfigBo bo) {
        LambdaQueryWrapper<RelaxGameConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }
    
    private LambdaQueryWrapper<RelaxGameConfig> buildQueryWrapper(RelaxGameConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RelaxGameConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RelaxGameConfig::getGameId);
        lqw.eq(StringUtils.isNotBlank(bo.getGameCode()), RelaxGameConfig::getGameCode, bo.getGameCode());
        lqw.like(StringUtils.isNotBlank(bo.getGameName()), RelaxGameConfig::getGameName, bo.getGameName());
        lqw.eq(StringUtils.isNotBlank(bo.getGameDesc()), RelaxGameConfig::getGameDesc, bo.getGameDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getGameIcon()), RelaxGameConfig::getGameIcon, bo.getGameIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getGamePath()), RelaxGameConfig::getGamePath, bo.getGamePath());
        lqw.eq(StringUtils.isNotBlank(bo.getGameCategory()), RelaxGameConfig::getGameCategory, bo.getGameCategory());
        lqw.eq(bo.getSortOrder() != null, RelaxGameConfig::getSortOrder, bo.getSortOrder());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), RelaxGameConfig::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getIsEnabled()), RelaxGameConfig::getIsEnabled, bo.getIsEnabled());
        return lqw;
    }
    
    /**
     * 新增小游戏配置
     *
     * @param bo 小游戏配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RelaxGameConfigBo bo) {
        RelaxGameConfig add = MapstructUtils.convert(bo, RelaxGameConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setGameId(add.getGameId());
        }
        return flag;
    }
    
    /**
     * 修改小游戏配置
     *
     * @param bo 小游戏配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RelaxGameConfigBo bo) {
        RelaxGameConfig update = MapstructUtils.convert(bo, RelaxGameConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
    
    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RelaxGameConfig entity) {
        //TODO 做一些数据校验,如唯一约束
    }
    
    /**
     * 校验并批量删除小游戏配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
